package com.steakliu.jsoup.jd;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2022-09-01  19:36
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Slf4j
@RequestMapping("/jd")
@RestController
public class JDController {
    @GetMapping("/get")
    public List<Map<String, String>> reptile() throws IOException {
        String url = "https://list.jd.com/list.html?cat=670%2C671%2C672&ev=15008_144488%5E&cid3=672";
        List<Map<String, String>> mapList = new ArrayList<>();
        // 获取JD商品列表
        Document document = Jsoup.connect(url).get();
        // 定位到商品列表标签
        Elements elements = document.select("div#J_goodsList > ul > li");
        for (Element element : elements) {
            // 商品详情
            Map<String, String> map = new HashMap<>(32);
            // 获取sku
            String sku = element.attr("data-sku");
            // 拼接详情页面url
            String detailUrl = "https://item.jd.com/" + sku + ".html";
            // 获取价格
            Element priceElement = element.select(".p-price > strong > i").first();
            if (priceElement != null) {
                map.put("price", priceElement.text());
            }

            // 使用该方法获取价格，获取过快，会报错。
            // String priceUrl = "https://p.3.cn/prices/mgets?skuIds=J_" + sku;
            // String priceJson= HttpUtil.get(priceUrl);
            // String result = JSON.parseArray(priceJson).get(0).toString();
            // JSONObject jsonObject = JSON.parseObject(result);
            // String price = jsonObject.get("p").toString();
            // map.put("price",price);

            // 获取每个商品的详情
            Document detailDoc = Jsoup.connect(detailUrl).get();
            // 获取标题
            String name = detailDoc.select("div.sku-name").text();
            map.put("name", name);
            // 详情
            Elements productElements = detailDoc.select("#detail > .tab-con > div");
            // 商品介绍
            Element productDescription = productElements.get(0);
            Elements parameters = productDescription.select(".p-parameter > ul");
            for (Element parameter : parameters) {
                Elements parameterElements = parameter.getElementsByTag("li");
                for (Element parameterElement : parameterElements) {
                    String text = parameterElement.text();
                    String[] split = text.split("：");
                    map.put(split[0], split[1]);
                }
            }
            mapList.add(map);
        }
        for (Map<String, String> map : mapList) {
            System.out.println(map);
        }
        return mapList;
    }
}
